package com.wenbo.fish.job;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.wenbo.fish.dao.TouchFishDAO;
import com.wenbo.fish.entity.SpiderInfo;
import com.wenbo.fish.entity.TouchFish;
import com.wenbo.fish.enums.SpiderServiceEnum;
import com.wenbo.fish.service.factory.SpiderFactory;
import com.wenbo.fish.utils.GsonUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @author changwenbo
 * @date 2025/1/8 19:58
 */
@Slf4j
@Component
public class SpiderJob {

    @Autowired
    private SpiderFactory spiderFactory;

    @Autowired
    private TouchFishDAO touchFishDao;

    @Scheduled(cron = "0 25 * * * ?")
    public void process() {
        try {
            spider();
        } catch (Exception e) {
            log.error("spider 定时任务执行失败 e = {}", e.getMessage(), e);
        }
    }

    private void spider() {
        for (SpiderServiceEnum spider : SpiderServiceEnum.values()) {
            try {
                List<SpiderInfo> list = spiderFactory.getSpiderService(spider).spider();
                insertSQL(spider, list);

                TimeUnit.SECONDS.sleep(10);
            } catch (Exception e) {
                log.error("spider 定时任务执行失败 spider = {}, e = {}", spider.getDesc(), e.getMessage(), e);
            }
        }
    }

    private void insertSQL(SpiderServiceEnum spider, List<SpiderInfo> list) {
        log.info("spider = {}, list = {}", spider.getDesc(), GsonUtils.toJson(list));
        if (CollectionUtils.isEmpty(list)) {
            return;
        }

        int cnt = getMaxCnt(spider.getName()) + 1;
        List<TouchFish> touchFishList = new ArrayList<>();
        for (SpiderInfo spiderInfo : list) {
            TouchFish touchFish = BeanUtil.toBean(spiderInfo, TouchFish.class);
            touchFish.setType(spider.getName());
            touchFish.setCnt(cnt + "");
            touchFishList.add(touchFish);
        }
        touchFishDao.insert(touchFishList);
    }

    private int getMaxCnt(String name) {
        try {
            LambdaQueryWrapper<TouchFish> wrapper = Wrappers.lambdaQuery();
            wrapper.eq(TouchFish::getType, name);
            wrapper.last(" order by id desc limit 1");
            TouchFish touchFish = touchFishDao.selectOne(wrapper);
            if (touchFish != null && StringUtils.isNotEmpty(touchFish.getCnt())) {
                log.info("getMaxCnt name = {}, cnt = {}", name, touchFish.getCnt());
                return Integer.parseInt(touchFish.getCnt());
            }
        } catch (Exception e) {
            log.warn("getMaxCnt name = {}, e = {}", name, e.getMessage(), e);
        }
        return 0;
    }
}